#include "Camera.h"

#include <qmatrix4x4>

void Camera::StrafeUp(double amount){
	m_vLookAt = m_vLookAt + m_vUp * amount;
}

void Camera::StrafeDown(double amount){
	m_vLookAt = m_vLookAt - m_vUp * amount;
}

void Camera::StrafeLeft(double amount){
	QVector3D v = QVector3D::crossProduct(m_vDirection, m_vUp);
	v.normalize();
	m_vLookAt = m_vLookAt - v * amount;
}

void Camera::StrafeRight(double amount){
	StrafeLeft(-amount);
}

void Camera::RotateUp(double theta){
	QVector3D axis = QVector3D::crossProduct(m_vDirection,m_vUp);
	QMatrix4x4 mat;

	axis.normalize();
	mat.setToIdentity();
	mat.rotate(theta,axis);

	m_vDirection = mat * m_vDirection;
	m_vUp = mat * m_vUp;
}

void Camera::RotateDown(double theta){
	RotateUp(-theta);
}

void Camera::RotateLeft(double theta){
	QVector3D axis = m_vUp;
	QMatrix4x4 mat;
	
	axis.normalize();
	mat.setToIdentity();
	mat.rotate(theta,axis);

	m_vDirection = mat * m_vDirection;
}

void Camera::RotateRight(double theta){
	RotateLeft(-theta);
}

void Camera::ZoomIn(double amount){
	m_fViewRadius -= amount;
	if(m_fViewRadius<m_fRadiusThreshold) m_fViewRadius = m_fRadiusThreshold;
}

void Camera::ZoomOut(double amount){
	ZoomIn(-amount);
}

void Camera::AbsoluteZoom(double amount){
	m_fViewRadius = amount;
}

QVector3D Camera::getCenter(){
	return m_vLookAt;
}

QVector3D Camera::getUp(){
	return m_vUp;
}

QVector3D Camera::getDirection(){
	return m_vDirection;
}

QVector3D Camera::getEye(){
	QVector3D eye;
	eye = m_vLookAt - m_vDirection * m_fViewRadius;
	return eye;
}